#include <stdio.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <time.h>

#include "../FSSJ/cmodel.h"
#include "../FSSJ/cmachine.h"
#include "../FSSJ/cbuffer.h"
#include "../FSSJ/cpart.h"
#include "../FSSJ/lib.h"

using namespace std;



extern int O_1[C_out+1][M+1];	//工艺矩阵，类别i在机器j上的工序编号

extern int p_[NR+1];				//工序i的加工时间，

extern int s_[NR+1][NR+1];		//工序i到j的切换时间

extern int 	R_0_c[n_0+1];	//需求序列类别号码
extern double 	R_0_d[n_0+1];	//需求序列交货期




/////全排列

int K[n_0+1];
int RS[n_0+1];//记录最后排列
double minT=-1.0;

ofstream ofs("/home/WANGMengChang/draft-SVN/Experiments/QiongJu_1/Debug/结果3.txt");

void clean()
{
	for (int j=0;j<=n_0;j++) {
		K[j]=0;
	}
}

void Init()
{
	for (int j=0;j<=n_0;j++) {
		K[j]=j;
		RS[j]=0;
	}
}

void showK()
{
/*	std::cout<<"Now Series: ";
	for (int j=1;j<=n_0;j++) {
		std::cout<<K[j]<<" ";
	}
	std::cout<<std::endl;
*/	
	ofs<<"P=";
	for (int j=1;j<=n_0;j++) {
		ofs<<K[j]<<"";
	}
	ofs<<std::endl;
}



double aTrial()
{
	set_data();

	CModel FlowShop;
	CBuffer B0;
	CMachine M1;
	CBuffer B1;
	CMachine M2;
	CBuffer B2;

	FlowShop.AddElement(&B0);
	FlowShop.AddElement(&M1);
	FlowShop.AddElement(&B1);
	FlowShop.AddElement(&M2);
	FlowShop.AddElement(&B2);

	M1.AddPreviousElement(&B0);
	M1.AddNextElement(&B1);
	M1.setMachineID(1);

	M2.AddPreviousElement(&B1);
	M2.AddNextElement(&B2);
	M2.setMachineID(2);

	CPart part[n_0];

	for (int i=0;i<n_0;i++) {
		part[i].setDueTime(R_0_d[K[i+1]]);
		part[i].setFinalType(R_0_c[K[i+1]]);
		//
		B0.GetAPartIn(&part[i]);
	}

	//////////////////////////////
	FlowShop.Run();
	showK();
	//B2.showContent();
	double tmp=B2.showDeltaTd(false);
	ofs<<"ΔTd="<<tmp<<std::endl;
	if(tmp<minT || minT<0){
		minT=tmp;
		for (int j=1;j<=n_0;j++) {
			RS[j]=K[j];
		}
	}
	return 0;
}

void TheExp()
{
	for (int i1=1;i1<=n_0;i1++) {
		clean();
		K[1]=i1;
		for (int i2=1;i2<=n_0;i2++) {
			if (i2==K[1]) continue;
			K[2]=i2;
			for (int i3=1;i3<=n_0;i3++) {
				if (i3==K[1] || i3==K[2]) continue;
				K[3]=i3;
				for (int i4=1;i4<=n_0;i4++) {
					if (i4==K[1] || i4==K[2] || i4==K[3]) continue;
					K[4]=i4;
					for (int i5=1;i5<=n_0;i5++) {
						if (i5==K[1] || i5==K[2] || i5==K[3] || i5==K[4]) continue;
						K[5]=i5;
						//////////////
						aTrial();
						//////////////
					}
				}
			}
		}
	}


}

///把上述多重循环转化为递归
//这是从网上找的算法，还没弄明白！！！！
// 2009-11-17

inline void Swap(int& i,int& j)
{
	int temp=i;
	i=j;
	j=temp;
}
//Pemu全排列
void Pemu(int start=1, int end=n_0)
{
	int i;
	if(start==end)
	{
		//一个排列生成了
		aTrial();
	}
	else
	{
		for(i=start;i<=end;i++)
		{
			Swap(K[start],K[i]);
			Pemu(start+1,end);
			Swap(K[start],K[i]);
		}
	}
}






int main(int argc, char **argv)
{
	std::cout<<"=====穷举试验======="<<std::endl;
	//clean();
	//TheExp2();
	
	Init();
	
	time_t startTime;
	time_t endTime;
	
	time(&startTime);
	Pemu();
	time(&endTime);
	
	std::cout<<"=======END===="<<std::endl;
	ofs<<"Start at :"<<ctime(&startTime)<<std::endl;
	ofs<<"End at :"<<ctime(&endTime)<<std::endl;
	
	ofs<<"minT P=";
	for (int j=1;j<=n_0;j++) {
		ofs<<RS[j]<<"";
	}
	ofs<<std::endl;
	ofs<<"min ΔTd="<<minT<<std::endl;

	return 0;
}



